package a_aa_amainpackage.Up_new.utils

import org.apache.spark.sql.{DataFrame, SQLContext, SparkSession}

/**
  * @Description TODO 
  * @Author liutaowei
  * @Date 2019-5-5 1:00
  */
object laozihao_util {

  /**
    * @ describe 老字号处理
    * @return
    */
  def  brand_add_laozihaoaddress (new_data:DataFrame,old_data:DataFrame,spark:SparkSession,brand_newAdd:String,sqlContext:SQLContext):DataFrame={

    val lao_1 = laozihao_ut_1(new_data,old_data,spark)
    val lao_2 = laozihao_modify(lao_1,spark,brand_newAdd,sqlContext)

    lao_2
  }

  /**
    * 这个代码是新增的品牌中没有老字号的数据打上老字号的标签
    * 如果老表中没有老字号的标签打上0
    * 如果老表中没有国家的标签打上-1
    * 如果老表中没有状态的标签打上0
    */
  private def laozihao_ut_1 (new_data:DataFrame,old_data:DataFrame,spark:SparkSession):DataFrame={
    old_data.registerTempTable("brand_statetables")
    val otherdata = new_data.drop("brand_state", "brand_type")
    otherdata.registerTempTable("otherdatas")

    val data = spark.sqlContext.sql(
      """
        |select
        |t1.*,
        |case when t2.brandName_cn is null then '0'  when brand_state= '-1' then '0' else brand_state end state,
        |case when t2.brandName_cn is null then '-1' else t2.brand_type end type
        |from
        |otherdatas t1
        |left join
        |brand_statetables t2
        |on t1.brandName_cn = t2.brandName_cn
      """.stripMargin).dropDuplicates("brandCcId")
      .drop("brand_state", "brand_type", "cn_stateother")
      .withColumnRenamed("state", "brand_state")
      .withColumnRenamed("type", "brand_type")
    //      println(s"${platform} :" + data.count())
    data

  }

  private def laozihao_modify(data:DataFrame,spark: SparkSession,brand_newAdd:String,sqlContext:SQLContext):DataFrame={
    data.registerTempTable("frames")
    //老字号字段修正
    val datas = sqlContext.sql(
      """
        |select
        |*,
        |
        |--泸州老窖----补充品牌名：国窖、泸州老酒坊
        |--北京龙徽酿酒----补充品牌名:  龙徽
        |case
        |when brandName rlike '国窖' and firstCategoryId = '10021' then '1'
        |when brandName rlike '泸州老酒坊' and firstCategoryId = '10021' then '1'
        |when brandName rlike '龙徽' and firstCategoryId = '10021' then '1'
        |when brandName rlike '龙徽' and firstCategoryId != '10021' then '0'
        |
        |--绍兴咸亨----太雕、泰雕、老顺泰
        |--五粮液----五粮头曲、五粮特曲、尖庄、五粮醇、六和液、五粮春
        |--恒源祥----彩羊、小囡（牌）
        |--江苏洋河----蓝色经典、梦之蓝
        |--广州酒家--秋之风、利口福
        |when brandName rlike '太雕' and firstCategoryId = '10021' then '1'
        |when brandName rlike '太雕' and firstCategoryId != '10021' then '0'
        |when brandName rlike '泰雕' and firstCategoryId = '10021' then '1'
        |when brandName rlike '老顺泰' and firstCategoryId = '10021' then '1'
        |
        |
        |when brandName rlike '五粮头曲' and firstCategoryId = '10021' then '1'
        |when brandName rlike '五粮特曲' and firstCategoryId = '10021' then '1'
        |when brandName rlike '尖庄' and firstCategoryId = '10021' then '1'
        |when brandName rlike '五粮醇' and firstCategoryId = '10021' then '1'
        |when brandName rlike '六和液' and firstCategoryId = '10021' then '1'
        |when brandName rlike '五粮春' and firstCategoryId = '10021' then '1'
        |
        |when brandName rlike '彩羊' and firstCategoryId = '10022' then '1'
        |when brandName rlike '彩羊' and firstCategoryId != '10022' then '0'
        |when brandName rlike '小囡' and (firstCategoryId = '10011'
        | or firstCategoryId = '10022') then '1'
        |when brandName rlike '小囡' and firstCategoryId != '10011'
        |and firstCategoryId != '10022' then '0'
        |
        |when brandName rlike '蓝色经典' and firstCategoryId = '10021' then '1'
        |when brandName rlike '梦之蓝' and firstCategoryId = '10021' then '1'
        |
        |when brandName rlike '秋之风' and firstCategoryId = '10021' then '1'
        |when brandName rlike '利口福' and firstCategoryId = '10021' then '1'
        |when brandName rlike '利口福' and firstCategoryId != '10021' then '0'
        |
        |
        |--凤凰有两个老字号：
        |-- 1、上海凤凰自行车：品牌-凤凰（牌） 自行车和电动车代步车等， 分类应该是运动户外--骑行运动
        |-- 2、上海凤凰毯业：品牌-凤凰， 毛毯等纺织产品，分类应该主要是家居家装--家纺
        |when brandName rlike '凤凰' and (firstCategoryId = '10013'
        |or firstCategoryId = '10022') then '1'
        |when brandName rlike '凤凰' and (firstCategoryId != '10013'
        |or firstCategoryId != '10022') then '0'
        |
        |
        |-- 万和老字号--仅为食品调料加工，酱油/簇/腐乳/咸菜等，现在统计的万和都是家用电器，是统计错了，万和的品牌名称：万和（牌）/恩安/黑颈鹤
        |-- 用万和牌能搜到一些商品
        |when brandName rlike '万和' and firstCategoryId = '10021' then '1'
        |when brandName rlike '万和' and firstCategoryId != '10021' then '0'
        |
        |--光明品牌对应四个老字号：
        |--扬州光明眼镜-- 光明
        |--上海开林漆厂--光明（牌）
        |--上海光明乳业--光明
        |--上海益民食品一厂：光明牌
        |--后两者可视为一家，都是光明集团
        |when brandName rlike '光明' and
        |(firstCategoryId = '10022' or firstCategoryId = '10017' or firstCategoryId = '10021') then '1'
        |when brandName rlike '光明' and
        |firstCategoryId != '10022' and firstCategoryId != '10017' and firstCategoryId != '10021' then '0'
        |
        |
        |--云南白药----补充：童俏俏、云丰、金口健、养元青
        |when brandName rlike '童俏俏' then '1'
        |when brandName rlike '云丰' then '1'
        |when brandName rlike '金口健' then '1'
        |when brandName rlike '养元青' then '1'
        |
        |
        |--周虎臣笔墨--品牌为虎牌、曹素功，只是笔墨，品类应该文具类
        |when brandName rlike '虎牌' and firstCategoryId = '10016'  then '1'
        |when brandName rlike '虎牌' and firstCategoryId != '10016'  then '0'
        |when brandName rlike '曹素功' and firstCategoryId = '10016'  then '1'
        |when brandName rlike '曹素功' and firstCategoryId != '10016'  then '0'
        |
        |
        |--
        |--杏花楼新雅粤菜--补充：新雅大厨
        |when brandName rlike '杏花楼' and firstCategoryId = '10021'  then '1'
        |when brandName rlike '杏花楼' and firstCategoryId != '10021'  then '0'
        |when brandName rlike '新雅大厨' and firstCategoryId = '10021'  then '1'
        |when brandName rlike '新雅大厨' and firstCategoryId != '10021'  then '0'
        |
        |
        |--张裕---补充：张裕金奖白兰地
        |when brandName rlike '张裕' and firstCategoryId = '10021'  then '1'
        |when brandName rlike '张裕' and firstCategoryId != '10021'  then '0'
        |--熊猫线缆--电线线缆相关商品
        |when brandName rlike '熊猫' and firstCategoryId = '10022'  then '1'
        |when brandName rlike '熊猫' and firstCategoryId != '10022'  then '0'
        |--三枪--
        |--THREE PISTOL/三枪 不是老字号， THREEGUN是老字号
        |when brandName rlike 'THREEGUN' and firstCategoryId = '10011'  then '1'
        |when brandName rlike 'THREEGUN' and firstCategoryId != '10011'  then '0'
        |when brandName rlike '三枪' then '0'
        |
        |--华昌----主要是珠宝首饰，其它行业不是老字号
        |when brandName rlike '华昌' and firstCategoryId = '10017'  then '1'
        |when brandName rlike '华昌' and firstCategoryId != '10017'  then '0'
        |--百乐--主要是酿造酱油醋等，百乐/PILOT是文具类不是老字号
        |when brandName rlike '百乐' and firstCategoryId = '10021'  then '1'
        |when brandName rlike '百乐' and firstCategoryId != '10021'  then '0'
        |--红星对应两个老字号：北京红星二锅头---红星
        |when brandName rlike '北京红星二锅头' and firstCategoryId = '10021'  then '1'
        |when brandName rlike '北京红星二锅头' and firstCategoryId != '10021'  then '0'
        |when brandName rlike '红星' and (firstCategoryId = '10021' or firstCategoryId = '10016')  then '1'
        |when brandName rlike '红星' and (firstCategoryId != '10021' and firstCategoryId != '10016') then '0'
        |--中国宣纸集团----红星（牌），红星墨液、古艺宣
        |--中国宣纸集团主要生产传统笔和宣纸等相关，其它红星只要不是二锅头酒的都不是老字号
        |
        |--保宁
        |when brandName rlike '保宁' then '0'
        |--恒源祥
        |when brandName rlike '恒源祥' then '1'
        |else brand_isLaoZiHao end lzh,
        |
        |--cn,en
        |case
        |when brandName_cn rlike '良品铺子' and firstCategoryId  ='10021' then '良品铺子/BESTORE' else brandName_cn end cn,
        |case
        |when brandName_cn rlike '良品铺子' and firstCategoryId  ='10021' then 'BESTORE/良品铺子' else brandName_en end en,
        |
        |--国家
        |case
        |when brandName_cn rlike '舒达' then '美国'
        |when brandName_cn rlike '优衣库' then '日本'
        |when brandName_cn rlike '雅诗兰黛' then '美国'
        |when brandName_cn rlike '华为' then '中国'
        |when brandName_cn rlike '荣耀' then '中国'
        |when brandName_cn rlike '苹果' then '美国'
        |when brandName_cn rlike '花花公子' then '美国'
        |when brandName_cn rlike 'THINKPAD' then '中国'
        |else brand_state end state,
        |
        |--品牌来源国
        |case
        |when brandName_cn rlike '舒达' then '外资品牌'
        |when brandName_cn rlike '优衣库' then '外资品牌'
        |when brandName_cn rlike '雅诗兰黛' then '外资品牌'
        |when brandName_cn rlike '华为' then '国产品牌'
        |when brandName_cn rlike '荣耀' then '国产品牌'
        |when brandName_cn rlike '苹果' then '外资品牌'
        |when brandName_cn rlike '花花公子' then '外资品牌'
        |when brandName_cn rlike 'THINKPAD' then '国产品牌'
        |else brand_type end type
        |
        |from
        |frames
      """.stripMargin)
      .drop("brand_isLaoZiHao", "brandName_cn", "brand_state", "brand_type","brandName_en")
      .withColumnRenamed("cn", "brandName_cn")
      .withColumnRenamed("state", "brand_state")
      .withColumnRenamed("type", "brand_type")
      .withColumnRenamed("lzh", "brand_isLaoZiHao")
      .withColumnRenamed("en", "brandName_en")
      .where("brandValueId != '-1' ")
      .dropDuplicates("brandCcId")
      datas.repartition(1).write.json(brand_newAdd)

    datas
  }
}
